package com.shop.model.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.model.entity.*;
import com.shop.model.mapper.ShopcartTbMapper;
import com.shop.model.service.IAttributeTbService;
import com.shop.model.service.IGoodsTbService;
import com.shop.model.service.IShopcartTbService;
import com.shop.model.service.IUserTbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shop
 * @since 2018-12-04
 */
@Controller

public class ShopcartTbController {
    @Autowired
    private IShopcartTbService iShopcartTbService;

    @Autowired
    private IUserTbService iUserTbService;

    @Autowired
    private IAttributeTbService iAttributeTbService;

    @Autowired
    private IGoodsTbService iGoodsTbService;

    @Autowired
    private ShopcartTbMapper shopcartTbMapper;

    //添加购物车 WSZ
    @ResponseBody
    @RequestMapping("/addShopCart")
    public Boolean addShopCart(HttpServletRequest request){
        //获取当前用户
        HttpSession session = request.getSession();
        //用户id
        int userId = (int)session.getAttribute("userId");
        //商品id 传
        String goodsId = request.getParameter("goodsId");
        //商品价格
        GoodsTb goodsTb = iGoodsTbService.getById(goodsId);
        BigDecimal price = goodsTb.getPrice();
        //商品数量  传
        String sgoodsNum = request.getParameter("goodsNum");
        Integer goodsNum = Integer.valueOf(sgoodsNum);
        BigDecimal big2 = new BigDecimal(goodsNum.toString());
        //商品小计
        BigDecimal multiply = price.multiply(big2);//multiply是BigDecimal的乘法运算
        //购买的属性  传
        String attributes = request.getParameter("attributes");
        System.out.println("-------------属性"+attributes);
        //添加到购物车数据库
       ShopcartTb shopcartTb =new ShopcartTb();
       shopcartTb.setCartGoodsAttributeId(attributes);
       shopcartTb.setCartGoodsId(Integer.valueOf(goodsId));
       shopcartTb.setCartGoodsNum(goodsNum);
       shopcartTb.setCartGoodsPrice(price);
       shopcartTb.setCartSubtotal(multiply);
       shopcartTb.setCartUserId(userId);
       boolean bool = iShopcartTbService.save(shopcartTb);
       return  bool;
    }

    //删除购物车 WSZ
    @ResponseBody
    @RequestMapping("/deleteShopCart")
    public Boolean deleteShopCart(HttpServletRequest request){
        System.out.println("进来了");
        //获取传过来的购物车id
        if(null!=request.getParameter("shopcartId")){
            String shopcartId = request.getParameter("shopcartId");
            Integer shopCartId = Integer.valueOf(shopcartId);
            //根据这个id去删除数据库数据
            boolean b = iShopcartTbService.remove(new QueryWrapper<ShopcartTb>()
                    .eq("shopcart_id", shopCartId));
            System.out.println("----"+b);
            return b;

        }
        return false;
    }

    //删除购物车所有 WSZ
    @ResponseBody
    @RequestMapping("/deleteAll")
    public Boolean deleteAll(HttpServletRequest request){
        boolean result=true;
        System.out.println("进来了");
        //获取传过来的购物车ids
        if(null!=request.getParameter("ids")){
            //这是一个以“，”分隔的id集合
            String ids = request.getParameter("ids");
            String[] idss = ids.split(",");
            for (String s : idss) {
                Integer shopCartId = Integer.valueOf(s);
                //通过这个id去删除购物车
                result = iShopcartTbService.remove(new QueryWrapper<ShopcartTb>()
                        .eq("shopcart_id", shopCartId));
            }
            return result;

        }
        return false;
    }


    //修改购物车某个商品的数量以及小计
    @ResponseBody
    @RequestMapping("/updateCount")
    public Boolean updateCount(HttpServletRequest request){
        if(null!=request.getParameter("shopCareId")&&null!=request.getParameter("count")){
            String shopCareId = request.getParameter("shopCareId");
            Integer shopCartId1 = Integer.valueOf(shopCareId);//购物车id
            //查询这个购物车信息
            ShopcartTb shopcartTb = iShopcartTbService.getOne(new QueryWrapper<ShopcartTb>().eq("shopcart_id", shopCartId1));
            String count = request.getParameter("count");
            Integer count1 = Integer.valueOf(count);
            //修改这个购物车
            shopcartTb.setCartGoodsNum(count1);//数量
            BigDecimal big2 = new BigDecimal(count1.toString());
            BigDecimal multiply = shopcartTb.getCartGoodsPrice().multiply(big2);//新小计
            shopcartTb.setCartSubtotal(multiply);//小计
            
            //修改
            boolean shopcart_id = iShopcartTbService.update(shopcartTb, new QueryWrapper<ShopcartTb>().eq("shopcart_id", shopCartId1));
            return  shopcart_id;
        }
        return false;
    }


    //显示购物车
    @RequestMapping("/showShopCart")
    public  String showShopCart(HttpServletRequest request, Model model){
        HttpSession session = request.getSession();
        HashMap<ShopcartTb,GoodsTb> hashMap=new HashMap();
        //获取当前用户id,这个用户来自登录
        if((null!=session.getAttribute("userId")&&null!=session.getAttribute("userName"))||null!=request.getParameter("userId")){
            int userId=0;
            if(null!=session.getAttribute("userId")){
                userId = (int)session.getAttribute("userId");
            }
            if(null!=request.getParameter("userId")){
                //解决阿里支付传过来的参数
                userId = Integer.valueOf(request.getParameter("userId"));
                //将这个id设置在session里面
                HttpSession session1 = request.getSession();
                session1.setAttribute("userId",userId);
            }
            System.out.println("------------------userid"+userId);
            String username =(String)session.getAttribute("userName");
            List<ShopcartTb> shopcartTblist= iShopcartTbService.list(new QueryWrapper<ShopcartTb>()
                    .eq("cart_user_id", userId));
            System.out.println("购物车长度-----------"+shopcartTblist.size());
            for (ShopcartTb shopcartTb : shopcartTblist) {
                Integer cartGoodsId = shopcartTb.getCartGoodsId();
                GoodsTb byId = iGoodsTbService.getById(cartGoodsId);
                hashMap.put(shopcartTb,byId);
            }


            model.addAttribute("size",hashMap.size());
            model.addAttribute("username",1);//前段做个判断，说明有用户
            model.addAttribute("hashMap",hashMap);
            return "shopcart";
        }

        //否则用户没登录
        model.addAttribute("username",0);//前段做个判断，说明没登录
        return "shopcart";
    }
}

